<?php
/**
 * 设置最底层接口
 * Enter description here ...
 * @author Administrator
 *
 */
abstract class Module implements IModule
{
    
    /**
     * 上传缩略图
     * 注意上传文件大小限制
     *
     * @return string $filePath 图片路径
     */
    protected function thumb($files)
    {
        if (is_uploaded_file($files['tmp_name'])) {
            $upfile = $files;
            $name = $upfile[name];
            $type = $upfile[type];
            $size = $upfile[size];
            $tmp_name = $upfile[tmp_name];
            $error = $upfile[error];
            switch ($type) {
                case 'image/jpeg' :
                    $ok = 1;
                    break;
                case 'image/pjpeg' :
                    $ok = 1;
                    break;
                case 'image/gif' :
                    $ok = 1;
                    break;
                case 'image/png' :
                    $ok = 1;
                    break;
                case 'image/x-png' :
                    $ok = 1;
                    break;
            }
            // 修正缩略图文件夹
            // 创建文件夹
            $save_path = getcwd()."/static/attached/thumb/";
            $save_url = "./static/attached/thumb/";
            $ym = date("Ym");
            $save_path .= $ym . "/";
            $save_url .= $ym . "/";
            if (! file_exists($save_path)) {
                mkdir($save_path);
            }
            
            if ($ok == '1' && $error == '0') {
                $fileType = substr($name, strpos($name, ".") + 1);
                $newName = 'thumbs_' . time() . "." . $fileType;
                $filepath = $save_path . $newName;
                move_uploaded_file($tmp_name, $filepath);
                
                return $save_url . $newName;
            }
        }
    }
    
    
    /**
     * 返回所有标签
     * Enter description here ...
     */
    public function getFlag()
    {
        return FlagModel::instance()->getFlag();
    }
    
    /**
     * 获取指定分类
     * Enter description here ...
     * @param int $id
     */
    public function getCategoryById($id)
    {
        return CategoryModel::instance()->getCateogryById($id);
    }
    
    /**
     * 递归获取子类 和本身
     *
     * @param int $id        	
     * @return mixed Ambigous mixed>
     */
    public function getSonCate($id = 0, $filterModule = 1)
    {
        $rs = CategoryModel::instance()->getcatagory($id);
        
        if (! $rs) {
            return $id;
        }
        foreach ($rs as $v) {
            
            if ($v['module'] != $filterModule) {
                continue;
            }
           
            $newids .= $v['id'] . ',';
        }
        
        return $newids . $id;
    
    }
    
    /**
     * 递归获取父类  这里没有采用左右值
     *
     * @param int $id        	
     * @return mixed Ambigous mixed>
     */
    public function getParentCate($id)
    {
        $ids = CategoryModel::instance()->getCateogryById($id);
        $newids = '';
        $newids .= $ids['id'] . ',';
        if ($ids['fid'] < 1) {
            return $id;
        }
        $rs = $this->getparentCate($ids['fid']);

        if (strlen($rs) > 0) {
            $newids .= $rs;
        }
        // $newids += $,0;v['id'] + ',';
        return $newids . $id;
    }
    /**
     * 全角转半角
     * @param String $str        	
     */
    protected function semiangle($str)
    {
        $arr = array(
                '０' => '0',
                '１' => '1',
                '２' => '2',
                '３' => '3',
                '４' => '4',
                '５' => '5',
                '６' => '6',
                '７' => '7',
                '８' => '8',
                '９' => '9',
                'Ａ' => 'A',
                'Ｂ' => 'B',
                'Ｃ' => 'C',
                'Ｄ' => 'D',
                'Ｅ' => 'E',
                'Ｆ' => 'F',
                'Ｇ' => 'G',
                'Ｈ' => 'H',
                'Ｉ' => 'I',
                'Ｊ' => 'J',
                'Ｋ' => 'K',
                'Ｌ' => 'L',
                'Ｍ' => 'M',
                'Ｎ' => 'N',
                'Ｏ' => 'O',
                'Ｐ' => 'P',
                'Ｑ' => 'Q',
                'Ｒ' => 'R',
                'Ｓ' => 'S',
                'Ｔ' => 'T',
                'Ｕ' => 'U',
                'Ｖ' => 'V',
                'Ｗ' => 'W',
                'Ｘ' => 'X',
                'Ｙ' => 'Y',
                'Ｚ' => 'Z',
                'ａ' => 'a',
                'ｂ' => 'b',
                'ｃ' => 'c',
                'ｄ' => 'd',
                'ｅ' => 'e',
                'ｆ' => 'f',
                'ｇ' => 'g',
                'ｈ' => 'h',
                'ｉ' => 'i',
                'ｊ' => 'j',
                'ｋ' => 'k',
                'ｌ' => 'l',
                'ｍ' => 'm',
                'ｎ' => 'n',
                'ｏ' => 'o',
                'ｐ' => 'p',
                'ｑ' => 'q',
                'ｒ' => 'r',
                'ｓ' => 's',
                'ｔ' => 't',
                'ｕ' => 'u',
                'ｖ' => 'v',
                'ｗ' => 'w',
                'ｘ' => 'x',
                'ｙ' => 'y',
                'ｚ' => 'z',
                '（' => '(',
                '）' => ')',
                '〔' => '[',
                '〕' => ']',
                '【' => '[',
                '】' => ']',
                '〖' => '[',
                '〗' => ']',
                '“' => '[',
                '”' => ']',
                '‘' => '[',
                '’' => ']',
                '｛' => '{',
                '｝' => '}',
                '《' => '<',
                '》' => '>',
                '％' => '%',
                '＋' => '+',
                '—' => '-',
                '－' => '-',
                '～' => '-',
                '：' => ':',
                '。' => '.',
                '、' => ',',
                '，' => '.',
                '、' => '.',
                '；' => ',',
                '？' => '?',
                '！' => '!',
                '…' => '-',
                '‖' => '|',
                '”' => '"',
                '’' => '`',
                '‘' => '`',
                '｜' => '|',
                '〃' => '"',
                '　' => ' ');
        
        return strtr($str, $arr);
    }

}